package top.linyuxb.lycloud.admin.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.Param;
import top.linyuxb.lycloud.admin.entity.BaseEntity;

import java.util.List;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/8/2 11:27
 * @Desc:
 *
 */
/*
     *复杂JPA操作  使用@Query()自定义sql语句  根据业务id UId去更新整个实体
     * 删除和更新操作，需要@Modifying和@Transactional注解的支持
     *
     * 更新操作中 如果某个字段为null则不更新，否则更新【注意符号和空格位置】
     *
     * @param huaYangArea   传入实体，分别取实体字段进行set
     * @return  更新操作返回sql作用条数
        @Modifying
        @Transactional
        @Query("update HuaYangArea hy set " +
                "hy.areaName = CASE WHEN :#{#huaYangArea.areaName} IS NULL THEN hy.areaName ELSE :#{#huaYangArea.areaName} END ," +
                "hy.areaPerson = CASE WHEN :#{#huaYangArea.areaPerson} IS NULL THEN hy.areaPerson ELSE :#{#huaYangArea.areaPerson} END ," +
                "hy.updateDate = CASE WHEN :#{#huaYangArea.updateDate} IS NULL THEN hy.updateDate ELSE :#{#huaYangArea.updateDate} END ," +
                "hy.updateId =  CASE WHEN :#{#huaYangArea.updateId} IS NULL THEN hy.updateId ELSE :#{#huaYangArea.updateId} END " +
                "where hy.uid = :#{#huaYangArea.uid}")
        int update(@Param("huaYangArea") HuaYangArea huaYangArea);
 */
@NoRepositoryBean
public interface BaseRepository<T extends BaseEntity, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
    /**
     * 根据状态全量查询
     * @param status
     * @return
     */
    List<T> findByStatusOrderByIdDesc(Integer status);

    /**
     * 通过id和状态查询
     *
     * @param id
     * @param status
     * @return
     */
    T findByIdAndStatus(Long id, Integer status);

}
